﻿using IBll;
using IDal;
using Model;
using System.Net.NetworkInformation;
using System;

namespace Bll
{
    public class HoomBll : IHoomBll
    {
        public HoomBll(IBaseDal<Hoom>hoomDal,IBaseDal<HoomType>hoomtypeDal)
        {
            HoomDal = hoomDal;
            HoomtypeDal = hoomtypeDal;
        }

        public IBaseDal<Hoom> HoomDal { get; }
        public IBaseDal<HoomType> HoomtypeDal { get; }

        //添加
        public async Task<int> AddHoom(Hoom hoom)
        {
            var weiyi = HoomDal.GetAsync().Any(x => x.HoomNumber == hoom.HoomNumber);
            if (!weiyi)
            {
                return await HoomDal.AddAsync(hoom);
            }
            else
            {
                return -2;
            }
        }
        //下拉框
        public List<HoomType> GetHoomType()
        {
            return HoomtypeDal.GetAsync().ToList();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="hoomNumber">编号</param>
        /// <param name="typeId">类型</param>
        /// <param name="createDate">创建时间</param>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        public dynamic GetHoom(string? hoomNumber, int typeId,DateTime?createDate, int index, int size)
        {
            var hoom = HoomDal.GetAsync().Where(x=>x.IsDel==false);
            var hoomtype= HoomtypeDal.GetAsync();
            var list = from a in hoom
                       join b in hoomtype
                       on a.TypeId equals b.TypeId
                       select new
                       {
                           a.Id,
                           a.HoomNumber,
                           a.TypeId,
                           a.Img,
                           a.State,
                           b.TypeName,
                           a.IsDel,
                           a.CreateUserId,
                           a.CreateDate,
                           a.UpdateUserId,
                           a.UpdateDate,
                           a.DeleteUserId,
                           a.DeleteDate
                       };
            if (!string.IsNullOrEmpty(hoomNumber))
            {
                list = list.Where(x => x.HoomNumber.Contains(hoomNumber));
            }
            if (typeId > 0)
            {
                list = list.Where(x => x.TypeId == typeId);
            }
            if (createDate != null)
            {
                var time=createDate.Value.AddDays(1);
                list = list.Where(x => x.CreateDate >= createDate && createDate < time);
            }
            var count = list.Count();
            var listcount=list.OrderBy(x=>x.Id).Skip((index-1)*size).Take(size).ToList();
            return new
            {
                count = count,
                listcount = listcount,
            };
        }
        //删除
        public async Task<int> DelHoom(int id)
        {
            var s = HoomDal.GetAsync().FirstOrDefault(x => x.Id == id);
            s.IsDel = true;
            s.DeleteDate = DateTime.Now;
            return await HoomDal.UpdaAsync(s);
        }
        //反填
        public Hoom FanTian(int id)
        {
            return HoomDal.GetAsync().FirstOrDefault(x => x.Id == id);
        }
        //修改
        public async Task<int> UpdaHoom(Hoom hoom)
        {
            hoom.UpdateDate = DateTime.Now;
            var weiyi = HoomDal.GetAsync().Any(x => x.HoomNumber == hoom.HoomNumber && x.Id == hoom.Id);
            if (!weiyi)
            {
                return await HoomDal.UpdaAsync(hoom);
            }
            else
            {
                return -2;
            }
        }
    }
}
